package ar.edu.itba.jMEBowling.scoring;

import java.util.ArrayList;

public class Scoring {
	ArrayList<Integer> pointsFirstShoot;
	ArrayList<Integer> pointsSecondShoot;
	ArrayList<Integer> pointsTotal;
	ArrayList<ScoreType> scoreTypes;
	private int shoot;
	private int finalResults;

	public Scoring() {
		pointsFirstShoot = new ArrayList<Integer>();
		pointsSecondShoot = new ArrayList<Integer>();
		pointsTotal = new ArrayList<Integer>();
		scoreTypes = new ArrayList<ScoreType>();
		this.finalResults = 0;
		this.shoot = 0;
	}

	public void addPointsInFirst(Integer pinsDown) {
		int thisPoints = pinsDown;

		if (thisPoints == 10) {
			pointsFirstShoot.add(shoot, 10);
			pointsSecondShoot.add(shoot, null);
			pointsTotal.add(shoot, 10);
			if (shoot - 1 >= 0
					&& scoreTypes.get(shoot - 1) == ScoreType.FULLWAIT) {
				pointsTotal.set(shoot - 1, 20);
				if (shoot - 2 >= 0
						&& scoreTypes.get(shoot - 2) == ScoreType.FULLWAIT) {
					pointsTotal.set(shoot - 2, 30);
					scoreTypes.set(shoot - 2, ScoreType.CALCULATED);

				}
			}
			if (shoot - 1 >= 0
					&& scoreTypes.get(shoot - 1) == ScoreType.HALFWAIT) {
				pointsTotal.set(shoot - 1, 20);
				scoreTypes.set(shoot - 1, ScoreType.CALCULATED);

			}
			scoreTypes.add(shoot, ScoreType.FULLWAIT);
			shoot++;

		} else {
			pointsFirstShoot.add(shoot, thisPoints);
			pointsTotal.add(shoot, thisPoints);
			if (shoot - 1 >= 0
					&& scoreTypes.get(shoot - 1) == ScoreType.HALFWAIT) {
				pointsTotal.set(shoot - 1, 10 + thisPoints);
				scoreTypes.set(shoot - 1, ScoreType.CALCULATED);

				if (shoot - 2 >= 0
						&& scoreTypes.get(shoot - 2) == ScoreType.FULLWAIT) {
					pointsTotal.set(shoot - 2, 20 + thisPoints);
					scoreTypes.set(shoot - 2, ScoreType.CALCULATED);

				}
			}

			// if (shoot - 2 >= 0
			// && scoreTypes.get(shoot - 2) == ScoreType.FULLWAIT) {
			// pointsTotal.set(shoot - 2, 20 + thisPoints);
			// scoreTypes.set(shoot - 2, ScoreType.CALCULATED);
			// }

		}
		finalResults = calculateResult();

	}

	private int calculateResult() {
		int points = 0;
		for (Integer point : pointsTotal) {
			points += point;
		}
		return points;
	}

	public void addPointsShootFinished(Integer pinsDown) {
		int thisPoints = pinsDown - pointsFirstShoot.get(shoot);
		pointsSecondShoot.add(shoot, thisPoints);
		pointsTotal.set(shoot, pointsFirstShoot.get(shoot)
				+ pointsSecondShoot.get(shoot));

		if (shoot - 1 >= 0 && scoreTypes.get(shoot - 1) == ScoreType.FULLWAIT) {
			pointsTotal.set(shoot - 1, 10 + thisPoints
					+ pointsFirstShoot.get(shoot));
			scoreTypes.set(shoot - 1, ScoreType.CALCULATED);
			finalResults += 10 + thisPoints + pointsFirstShoot.get(shoot);
		}

		if (pinsDown == 10) {
			scoreTypes.add(shoot, ScoreType.HALFWAIT);
		} else {
			scoreTypes.add(shoot, ScoreType.CALCULATED);
			finalResults += pointsFirstShoot.get(shoot)
					+ pointsSecondShoot.get(shoot);
		}
		shoot++;
		finalResults = calculateResult();
	}

	public String toString() {
		int i = 0;
		String result = null;
		while (i != shoot) {
			if (pointsSecondShoot == null) {
				result += pointsTotal.get(i) + "\n";
			} else {
				result += pointsTotal.get(i) + "=> (" + pointsFirstShoot.get(i)
						+ "," + pointsSecondShoot.get(i) + ")\n";
			}
			i++;
		}
		return result;
	}

	public ArrayList<Integer> getPointsFirstShoot() {
		return pointsFirstShoot;
	}

	public void setPointsFirstShoot(ArrayList<Integer> pointsFirstShoot) {
		this.pointsFirstShoot = pointsFirstShoot;
	}

	public ArrayList<Integer> getPointsSecondShoot() {
		return pointsSecondShoot;
	}

	public void setPointsSecondShoot(ArrayList<Integer> pointsSecondShoot) {
		this.pointsSecondShoot = pointsSecondShoot;
	}

	public ArrayList<Integer> getPointsTotal() {
		return pointsTotal;
	}

	public void setPointsTotal(ArrayList<Integer> pointsTotal) {
		this.pointsTotal = pointsTotal;
	}

	public ArrayList<ScoreType> getScoreTypes() {
		return scoreTypes;
	}

	public void setScoreTypes(ArrayList<ScoreType> scoreTypes) {
		this.scoreTypes = scoreTypes;
	}

	public int getShoot() {
		return shoot;
	}

	public void setShoot(int shoot) {
		this.shoot = shoot;
	}

	public int getFinalResults() {
		return finalResults;
	}

	public void setFinalResults(int finalResults) {
		this.finalResults = finalResults;
	}

}
